package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feigns.ApUserFeign;
import com.heima.feigns.ArticleFeign;
import com.heima.feigns.WemediaFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 查询用户认证列表~
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AuthDTO dto) {
        //校验数据合法性
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //校验数据的状态
        LambdaQueryWrapper<ApUserRealname> wrapper = Wrappers.<ApUserRealname>lambdaQuery();
        if (dto.getStatus() != null){
        wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        //初始化分页
        dto.checkParam();
        //根据分页查询结果
        Page page = new Page(dto.getPage(),dto.getSize());
        IPage result = this.page(page, wrapper);
        //封装结果返回
        PageResponseResult rs = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), result.getRecords());

        return rs;
    }

    /**
     * 用户驳回
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDTO dto) {
        //校验数据合法性
        if (dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询数据  判断数据当前的状态是否为1 待审核
        ApUserRealname user = this.getById(dto.getId());
        //判断使用是否存在
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前用户不存在!");
        }
        //判断用户状态是否为待审核
        if (user.getStatus() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"数据已被审核");
        }
        //修改数据状态信息
        LambdaQueryWrapper<ApUserRealname> wrapper = Wrappers.<ApUserRealname>lambdaQuery();
        //定义失败状态
        short stat = 2 ;
        user.setStatus(stat);
        user.setUpdatedTime(new Date());
        user.setReason(dto.getMsg());
        this.updateById(user);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    ApUserFeign apUserFeign;
    /**
     * 认证通过
     * @param dto
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult authPass(AuthDTO dto) {
        //校验数据合法性
        if (dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询数据  判断数据当前的状态是否为1 待审核
        ApUserRealname user = this.getById(dto.getId());
        //判断使用是否存在
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前用户不存在!");
        }
        //判断用户状态是否为待审核
        if (user.getStatus() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"数据已被审核");
        }
        //定义成功状态
        short stat = 9 ;
        user.setStatus(stat);
        user.setUpdatedTime(new Date());
        this.updateById(user);
        //根据Id查询User信息
        ApUser apUser = apUserFeign.findApUserById(user.getUserId());
        //调用feign判断自媒体账号是否存在
        int i = wemediaFeign.findByName(user.getName());
        if (i > 0 ){
            //表示已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }else{

            //创建自媒体账号
            WmUser wmUser = new WmUser();
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setType(0);
            wmUser.setApUserId(apUser.getId());
            wmUser.setStatus(9);
            wmUser.setCreatedTime(new Date());
            wemediaFeign.save(wmUser);
        }
        //调用feign查询作者账号是否存在
        int i1 = articleFeign.findByUserName(user.getName());
        if (i1 != 0 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"作者账户已被注册");
        }else {
            //注册作者账户
            ApAuthor apAuthor = new ApAuthor();
            apAuthor.setUserId(apUser.getId());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setName(apUser.getName());
            apAuthor.setType(2);
            apAuthor.setUserId(apUser.getId());
            articleFeign.save(apAuthor);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
